package com.dadibadi.core.auth.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dadibadi.core.auth.qo.UserAccountEditQO;
import com.dadibadi.core.auth.vo.UserAccountDetailVO;
import com.dadibadi.core.auth.vo.UserAccountListVO;
import com.dadibadi.core.common.Constants;
import com.dadibadi.core.common.exception.MyBizException;
import com.dadibadi.core.common.utils.MD5Util;
import com.dadibadi.core.dao.entity.SysRoleTab;
import com.dadibadi.core.dao.entity.UserAccountTab;
import com.dadibadi.core.dao.mapper.UserAccountMapper;
import com.dadibadi.core.user.service.UserInfoService;
import com.venusoft.developer.common.utils.ConvertUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author adaaccio
 */
@Service
@Slf4j
public class UserAccountService {

    @Resource
    UserAccountMapper userAccountMapper;

    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @Resource
    UserInfoService userInfoService;


    /*@Value("${spring.profiles.active}")
    private String env;*/

    /**
     * 查找用户邮箱，如果不存在，则报错
     */
    public UserAccountDetailVO getUserAccount(UserAccountEditQO qo) {
        UserAccountTab userAccountTab;

        userAccountTab = userAccountMapper.selectOne(
                Wrappers.<UserAccountTab>lambdaQuery()
                        .eq(UserAccountTab::getEmail, qo.getEmail())
        );
        if (userAccountTab == null) {
            throw MyBizException.ACCOUNT_NOT_EXIST_ERROR;
        } else {

            return ConvertUtil.convert(userAccountTab, UserAccountDetailVO.class);

        }
    }

    /**
     * 保存用户账号信息 用于注册
     */
    public UserAccountTab saveUserAccount(UserAccountEditQO qo) {

        UserAccountTab userAccountTab;
        UserAccountTab userAccountTab1 = new UserAccountTab();
        userAccountTab = userAccountMapper.selectOne(
                Wrappers.<UserAccountTab>lambdaQuery()
                        .eq(UserAccountTab::getEmail, qo.getEmail())
        );
        if (userAccountTab != null) {
            throw MyBizException.EMAIL_EXIST_ERROR;
        } else {
            String password = MD5Util.sha256Hex(qo.getPassword());
            userAccountTab1.setPassword(password);
            userAccountTab1.setEmail(qo.getEmail());
            userAccountMapper.insert(userAccountTab1);
            SysRoleTab sysRoleTab = new SysRoleTab();
        }

        return userAccountTab1;
    }

    public void attachAdminToken(UserAccountListVO vo) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        String bearer = (String) redisTemplate.opsForValue().get(Constants.ADMIN_BEARER + vo.getUserId());

        String newBearer = Constants.BEARER_ADMIN + uuid;

        vo.setToken(newBearer);
        redisTemplate.opsForValue().setIfAbsent(newBearer, vo, 2, TimeUnit.DAYS);

        redisTemplate.opsForValue().set(Constants.ADMIN_BEARER + vo.getUserId(), newBearer, 2, TimeUnit.DAYS);
    }
}

